// pages/cart/index.js


import {getSetting,chooseAddress,openSetting,showModal,showToast} from "../../utils/asyncWx";
import regeneratorRuntime from '../../lib/runtime/runtime';
Page({
  data:{
    address:{},
    cart:[],
    allchecked:false,
    totalPrice:0,
    totalNum:0
  },
  onShow: function () {
    //1获取缓存中的收货地址信息
    const address = wx.getStorageSync("address");
    const cart= wx.getStorageSync("cart")||[];

    this.setData({
      address
    });
    this.setCart(cart);

    // const cart = wx.getStorageSync('cart')||[];//当值不存在时，将其定义为一个空数组，相当于三元表达式的写法
    // // 判断购物车是否有数据，回显图片展示  
    // if(cart.length>0){
    //   this.setData({
    //     hintStatus:false
    //   })
    // }
    // this.setData({address});
    // this.setCart(cart);
    // // 全选效果的 实现
    // for(var i= 0;i<cart.length;i++){
    //   if(cart[i].checked == true){
    //       this.setData({
    //         allChecked:true
    //       })  
    //   }else{
    //     this.data.allChecked = false;
    //   }
    // }
  },
  // 1点击收货地址触发的事件  wx.chooseAddress
  async handleChooseAddress(){
    try {
    // 1、获取权限状态
    const res1 =await getSetting();
    const scopeAddress = res1.authSetting['scope.address'];
        // 2、判断权限状态
        if(scopeAddress == false){
          await openSetting();
        }
        const res2 = await chooseAddress();
        wx.setStorageSync('address', res2);
        wx.setStorageSync('address1', res2.provinceName+res2.cityName+res2.countyName+res2.detailInfo);
  }catch (error) {
    console.log('error',error)
    }
  },
  handleItemChange(e){
    // 获取被修改的商品id
    const goods_id=e.currentTarget.dataset.id;
    // console.log(goods_id)
// 获取购物车数组
    let {cart} =this.data;
    // 找到被修改的商品对象
    let index= cart.findIndex(v=>v.goods_id===goods_id);
    // 选中状态取反
    cart[index].checked=!cart[index].checked;

    this.setCart(cart);
  },
  // 设置购物车状态，重新计算价格
  setCart(cart){
    // 计算全选
    const allchecked=cart.length?cart.every(v=>v.checked):false;
    // 1、获取缓存中的购物车数据
    let totalPrice=0;
    let totalNum=0;
    cart.forEach(v=>{
      if(v.checked){
        totalPrice+=v.num*v.goods_price;
        totalNum+=v.num;
      }
    })
    this.setData({
      cart,
      allchecked,
      totalPrice,
      totalNum
    })
    wx.setStorageSync("cart", cart);
      
  },
  handleItemAllChange(){
    // 获取data中的数据
    let {cart,allchecked}= this.data;
    allchecked=!allchecked;
    cart.forEach(v=>v.checked=allchecked);
    this.setCart(cart);
  },
  async handleItemNumEdit(e){
    const {operation,id} =e.currentTarget.dataset;
    let {cart} =this.data;
    const index= cart.findIndex(v=>v.goods_id===id);

    if(cart[index].num===1&&operation===-1){
      const res= await showModal({content:"您是否要删除该商品！"});
      if(res.confirm){
        cart.splice(index,1);
        this.setCart(cart);
      }
    }else{
      cart[index].num+=operation;
      this.setCart(cart); 
    }
  },
  // 支付结算页面
  async handlepay(){
    const {address,totalNum} =this.data;
    if(!address.userName){
      await showToast({title:"您没有选择收货地址"});
      return;
    }
    if(totalNum===0){
      await showToast({title:"您没有选购商品"});
      return;
    }
    wx.navigateTo({
      url: '/pages/pay/index'
    });
      
  }
})